<template>
  <div class="main-scroll">
    <headerc :title="title"></headerc>
    <el-card class="box-card">
      <!-- <div class="title-text">数据筛选</div> -->
      <div style="padding: 10px" class="c-filter">
        <el-form
          ref="filterForm"
          :inline="true"
          :model="filterForm"
          class="demo-form-inline"
          label-width="70px"
        >
          <el-form-item label="员工姓名">
            <el-input
              v-model="filterForm.emplName"
              placeholder="员工姓名"
            ></el-input>
          </el-form-item>

          <el-form-item label="原车间">
            <el-input
              v-model="filterForm.oldWorkshop"
              placeholder="原车间"
            ></el-input>
          </el-form-item>

          <el-form-item label="原班组">
            <el-input
              v-model="filterForm.oldTeam"
              placeholder="原班组"
            ></el-input>
          </el-form-item>

          <el-form-item label="原职务">
            <el-input
              v-model="filterForm.oldPost"
              placeholder="原职务"
            ></el-input>
            <!-- <el-select v-model="filterForm.deptTypeName1" placeholder="请选择">
              <el-option label="部门一" value="shanghai"></el-option>
              <el-option label="部门二" value="beijing"></el-option>
            </el-select> -->
          </el-form-item>

          <el-form-item label="现车间">
            <el-input
              v-model="filterForm.currentWorkshop"
              placeholder="现车间"
            ></el-input>
          </el-form-item>

          <el-form-item label="现班组">
            <el-input
              v-model="filterForm.currentTeam"
              placeholder="现班组"
            ></el-input>
          </el-form-item>

          <el-form-item label="现职务">
            <el-input
              v-model="filterForm.currentPost"
              placeholder="现职务"
            ></el-input>
            <!-- <el-select v-model="filterForm.deptTypeName3" placeholder="请选择">
              <el-option label="班组一" value="shanghai"></el-option>
              <el-option label="班组二" value="beijing"></el-option>
            </el-select> -->
          </el-form-item>

          <el-form-item label="变更时间">
            <el-date-picker
              v-model="filterForm.changeTime"
              type="daterange"
              range-separator="至"
              start-placeholder="起始日期"
              end-placeholder="截止日期"
              value-format="yyyy-MM-dd"
              @change="changeDate"
            >
            </el-date-picker>
          </el-form-item>
          <el-row>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="query"
                >查询</el-button
              >
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="reset">清除查询条件</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </div>
    </el-card>
    <el-card class="c-main-show">
      <!-- <div class="main-seting">
        <div> -->
      <!-- <div class="title-text">数据列表</div> -->
      <el-button
        v-auth="'C07002'"
        @click="exportExcel"
        style="
          float: right;
          margin-right: 20px;
          margin-bottom: 20px;
          margin-top: 20px;
        "
        icon="el-icon-download"
        >导出</el-button
      >
      <el-button
        v-auth="'C07001'"
        type="primary"
        style="
          float: right;
          margin-right: 20px;
          margin-bottom: 20px;
          margin-top: 20px;
        "
        icon="el-icon-plus"
        @click="goAddPostchange"
        >添加</el-button
      >
      <div style="margin: 20px; min-height: 55vh">
        <el-table
          :data="postchangeList"
          border
          style="width: 100%"
          :header-cell-style="{ background: '#F6F7FB' }"
        >
          <el-table-column
            type="index"
            :index="indexAdd"
            label="序号"
            width="50"
          >
          </el-table-column>
          <el-table-column label="员工姓名">
            <template slot-scope="scope">
              <p>{{ scope.row.emplName }}</p>
            </template>
          </el-table-column>
          <el-table-column label="变更时间">
            <template slot-scope="scope">
              <p>{{ scope.row.changeTime | formatDate('yyyy-MM-dd') }}</p>
            </template>
          </el-table-column>
          <el-table-column label="变更用户">
            <template slot-scope="scope">
              <p>{{ scope.row.changeUserName }}</p>
            </template>
          </el-table-column>
          <el-table-column label="原车间">
            <template slot-scope="scope">
              <p>{{ scope.row.oldWorkshop }}</p>
            </template>
          </el-table-column>
          <el-table-column label="原班组">
            <template slot-scope="scope">
              <p>{{ scope.row.oldTeam }}</p>
            </template>
          </el-table-column>
          <el-table-column label="原职务">
            <template slot-scope="scope">
              <p>{{ scope.row.oldPost }}</p>
            </template>
          </el-table-column>
          <el-table-column label="现车间">
            <template slot-scope="scope">
              <p>{{ scope.row.currentWorkshop }}</p>
            </template>
          </el-table-column>
          <el-table-column label="现班组">
            <template slot-scope="scope">
              <p>{{ scope.row.currentTeam }}</p>
            </template>
          </el-table-column>
          <el-table-column label="现职务">
            <template slot-scope="scope">
              <p>{{ scope.row.currentPost }}</p>
            </template>
          </el-table-column>
          <el-table-column label="备注" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <p>{{ scope.row.remark }}</p>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="isUpdate"
        background
        layout="prev, pager, next"
        :page-size="pageSize"
        :current-page="pageNo"
        :total="total"
        :hide-on-single-page="total < 10"
        @current-change="currentChange"
        style="margin-left: 25%"
      >
      </el-pagination>
      <!-- </div>
      </div> -->
    </el-card>
    <el-dialog
      title="添加"
      :visible.sync="dialogVisible"
      @close="cancel"
      width="50%"
    >
      <el-form
        ref="createForm"
        :model="createForm"
        :rules="rules"
        label-width="200px"
      >
        <el-form-item label="变更时间:" prop="changeTime">
          <el-date-picker
            v-model="createForm.changeTime"
            type="date"
            placeholder="变更时间"
            value-format="yyyy-MM-dd"
            @change="changeDate2"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="变更用户" prop="changeUserId">
          <el-select
            v-model="createForm.changeUserId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="changeUserRemoteMethod"
            :loading="changeUserLoading"
            clearable
          >
            <el-option
              v-for="item in changeUserOptions"
              :key="item.id"
              :label="item.emplName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原车间:" prop="oldWorkshop">
          <el-select
            v-model="createForm.oldWorkshop"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="oldWorkRemoteMethod"
            :loading="oldWorkLoading"
            clearable
          >
            <el-option
              v-for="item in oldWorkOptions"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原班组:" prop="oldTeam">
          <el-select
            v-model="createForm.oldTeam"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="oldTeamRemoteMethod"
            :loading="oldTeamLoading"
            clearable
          >
            <el-option
              v-for="item in oldTeamOptions"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
          <!-- <el-input
            v-model="createForm.oldTeam"
            :rows="4"
            placeholder="请填写"
            style="width: 300px;"
          ></el-input> -->
        </el-form-item>
        <el-form-item label="原职务:" prop="oldPost">
          <el-select
            v-model="createForm.oldPost"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="oldPostRemoteMethod"
            :loading="oldPostLoading"
            clearable
          >
            <el-option
              v-for="item in oldPostOptions"
              :key="item.id"
              :label="item.postName"
              :value="item.postName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="现车间:" prop="currentWorkshop">
          <el-select
            v-model="createForm.currentWorkshop"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="currentWorkRemoteMethod"
            :loading="currentWorkLoading"
            clearable
          >
            <el-option
              v-for="item in currentWorkOptions"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="现班组:" prop="currentTeam">
          <el-select
            v-model="createForm.currentTeam"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="currentTeamRemoteMethod"
            :loading="currentTeamLoading"
            clearable
          >
            <el-option
              v-for="item in currentTeamOptions"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="现职务:" prop="currentPost">
          <el-select
            v-model="createForm.currentPost"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="currentPostRemoteMethod"
            :loading="currentPostLoading"
            clearable
          >
            <el-option
              v-for="item in currentPostOptions"
              :key="item.id"
              :label="item.postName"
              :value="item.postName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注:">
          <el-input v-model="createForm.remark" placeholder="请填写"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addPostchangeList">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import headerc from '@/components/header.vue'
import { postchangeList, addPostchange } from '@/api/personal-files'
import { exportExcel, getlist } from '@/api'
import { exportMethod } from '@/utils/utils'
import { positionList, deptList } from '@/api/permission'
export default {
  components: {
    headerc
  },
  data() {
    return {
      isUpdate: true,
      title: '岗位变更登记栏',
      value1: '',
      changeUserOptions: [],
      oldWorkOptions: [],
      oldTeamOptions: [],
      oldPostOptions: [],
      currentWorkOptions: [],
      currentTeamOptions: [],
      currentPostOptions: [],
      changeUserLoading: false,
      oldWorkLoading: false,
      oldTeamLoading: false,
      oldPostLoading: false,
      currentWorkLoading: false,
      currentTeamLoading: false,
      currentPostLoading: false,
      filterForm: {
        changeTime: '',
        emplName: '',
        oldWorkshop: '',
        oldTeam: '',
        oldPost: '',
        currentWorkshop: '',
        currentTeam: '',
        currentPost: ''
      },
      createForm: {
        changeTime: new Date(),
        oldWorkshop: '',
        oldTeam: '',
        oldPost: '',
        currentWorkshop: '',
        currentTeam: '',
        currentPost: '',
        remark: ''
      },
      rules: {
        changeTime: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ],
        changeUserId: [
          { required: true, message: '请选择变更用户', trigger: 'blur' }
        ],
        oldWorkshop: [
          { required: true, message: '请填写原车间', trigger: 'blur' }
        ],
        oldTeam: [{ required: true, message: '请填写原班组', trigger: 'blur' }],
        oldPost: [{ required: true, message: '请填写原职务', trigger: 'blur' }],
        currentWorkshop: [
          { required: true, message: '请填写现车间', trigger: 'blur' }
        ],
        currentTeam: [
          { required: true, message: '请填写现班组', trigger: 'blur' }
        ],
        currentPost: [
          { required: true, message: '请填写现职务', trigger: 'blur' }
        ]
      },
      pageNo: 1, // 分页页码
      pageSize: 10, // 每页条数
      total: 0,
      postchangeList: [],
      dialogVisible: false
    }
  },
  created() {
    this.getPostchangeList()
  },
  methods: {
    exportExcel() {
      const data = {
        changeEndTime: this.filterForm.changeTime[1],
        changeStartTime: this.filterForm.changeTime[0],
        currentPost: this.filterForm.currentPost,
        currentTeam: this.filterForm.currentTeam,
        currentWorkshop: this.filterForm.currentWorkshop,
        emplName: this.filterForm.emplName,
        oldPost: this.filterForm.oldPost,
        oldTeam: this.filterForm.oldTeam,
        oldWorkshop: this.filterForm.oldWorkshop,
        pageNo: this.pageNo,
        pageSize: 5000
      }
      exportMethod('postChange', data, '岗位变更记录')
    },
    goAddPostchange() {
      this.dialogVisible = true
      //  this.createForm.changeTime = new Date()
    },
    async currentPostRemoteMethod(query) {
      if (query !== '') {
        this.currentPostLoading = true
        const res = await positionList({
          pageNo: 1,
          pageSize: 50,
          postName: query
        })
        if (res.code == 200) {
          console.log('res', res)
          this.currentPostLoading = false
          this.currentPostOptions = res.data.list
        }
      } else {
        this.currentPostOptions = []
      }
    },
    async currentTeamRemoteMethod(query) {
      if (query !== '') {
        this.currentTeamLoading = true
        const res = await deptList({
          isPage: false,
          deptLevel: 4,
          pageNo: 1,
          pageSize: 50,
          deptName: query
        })
        if (res.code == 200) {
          console.log('res', res)
          this.currentTeamLoading = false
          this.currentTeamOptions = res.data.list
        }
      } else {
        this.currentTeamOptions = []
      }
    },
    async currentWorkRemoteMethod(query) {
      if (query !== '') {
        this.currentWorkLoading = true
        const res = await deptList({
          isPage: false,
          deptLevel: 2,
          pageNo: 1,
          pageSize: 50,
          deptName: query
        })
        if (res.code == 200) {
          this.currentWorkLoading = false
          this.currentWorkOptions = res.data.list
        }
      } else {
        this.currentWorkOptions = []
      }
    },
    async changeUserRemoteMethod(query) {
      if (query !== '') {
        this.changeUserLoading = true
        const res = await getlist({ emplName: query })
        if (res.code == 200) {
          console.log('res', res)
          this.changeUserLoading = false
          this.changeUserOptions = res.data
        }
      } else {
        this.changeUserOptions = []
      }
    },
    async oldWorkRemoteMethod(query) {
      if (query !== '') {
        this.oldWorkLoading = true
        const res = await deptList({
          isPage: false,
          deptLevel: 2,
          pageNo: 1,
          pageSize: 50,
          deptName: query
        })
        if (res.code == 200) {
          console.log('res', res)
          this.oldWorkLoading = false
          this.oldWorkOptions = res.data.list
        }
      } else {
        this.oldWorkOptions = []
      }
    },
    async oldTeamRemoteMethod(query) {
      if (query !== '') {
        this.oldTeamLoading = true
        const res = await deptList({
          isPage: false,
          deptLevel: 4,
          pageNo: 1,
          pageSize: 50,
          deptName: query
        })
        if (res.code == 200) {
          this.oldTeamLoading = false
          this.oldTeamOptions = res.data.list
          // console.log('this.positionOptions', this.positionOptions)
        }
      } else {
        this.oldTeamOptions = []
      }
    },
    async oldPostRemoteMethod(query) {
      if (query !== '') {
        this.oldPostLoading = true
        const res = await positionList({
          pageNo: 1,
          pageSize: 50,
          postName: query
        })
        if (res.code == 200) {
          this.oldPostLoading = false
          this.oldPostOptions = res.data.list
          // console.log('this.positionOptions', this.positionOptions)
        }
      } else {
        this.oldPostOptions = []
      }
    },
    // async exportExcel() {
    //   const res = await exportExcel('postChange', {
    //     changeEndTime: this.filterForm.changeTime[1],
    //     changeStartTime: this.filterForm.changeTime[0],
    //     currentPost: this.filterForm.currentPost,
    //     currentTeam: this.filterForm.currentTeam,
    //     currentWorkshop: this.filterForm.currentWorkshop,
    //     emplName: this.filterForm.emplName,
    //     oldPost: this.filterForm.oldPost,
    //     oldTeam: this.filterForm.oldTeam,
    //     oldWorkshop: this.filterForm.oldWorkshop,
    //     pageNo: 1,
    //     pageSize: 50
    //   })
    //  let blob = new Blob([res.data]); // { type: "application/vnd.ms-excel" }
    // let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
    // // 创建url之后可以模拟对此文件对象的一系列操作，例如：预览、下载
    // let a = document.createElement("a");
    // a.href = url;
    // a.download = "表格.xlsx";
    // a.click();
    // window.URL.revokeObjectURL(url);
    //   console.log('res', res)
    // },
    cancel() {
      this.dialogVisible = false
      this.$refs.createForm.resetFields()
      this.createForm.remark = ''
    },
    // 序号-页面切换递增
    indexAdd(index) {
      return index + 1 + (this.pageNo - 1) * this.pageSize
    },
    addPostchangeList() {
      this.$refs.createForm.validate(async (valid) => {
        if (valid) {
          const res = await addPostchange({
            changeTime: this.createForm.changeTime,
            changeUserId: this.createForm.changeUserId,
            currentPost: this.createForm.currentPost,
            currentTeam: this.createForm.currentTeam,
            currentWorkshop: this.createForm.currentWorkshop,
            oldPost: this.createForm.oldPost,
            oldTeam: this.createForm.oldTeam,
            oldWorkshop: this.createForm.oldWorkshop,
            remark: this.createForm.remark
          })
          if (res.code == 200) {
            this.$message({
              message: '创建成功',
              type: 'success'
            })
            this.dialogVisible = false
            this.getPostchangeList()
          }
          // console.log('res', res)
        } else {
          return false
        }
      })
    },
    changeDate2(date) {
      this.createForm.changeTime = date
    },
    query() {
      this.pageNo = 1
      this.isUpdate = false
      this.getPostchangeList()
    },
    reset() {
      // this.$refs.filterForm.resetField()
      this.filterForm.changeTime = ''
      this.filterForm.emplName = ''
      this.filterForm.oldWorkshop = ''
      this.filterForm.oldTeam = ''
      this.filterForm.oldPost = ''
      this.filterForm.currentWorkshop = ''
      this.filterForm.currentTeam = ''
      this.filterForm.currentPost = ''
    },
    currentChange(pageNo) {
      this.pageNo = pageNo
      this.getPostchangeList()
    },
    changeDate(date) {
      this.filterForm.changeTime = date
    },
    async getPostchangeList() {
      const res = await postchangeList({
        changeEndTime: this.filterForm.changeTime[1],
        changeStartTime: this.filterForm.changeTime[0],
        currentPost: this.filterForm.currentPost,
        currentTeam: this.filterForm.currentTeam,
        currentWorkshop: this.filterForm.currentWorkshop,
        emplName: this.filterForm.emplName,
        oldPost: this.filterForm.oldPost,
        oldTeam: this.filterForm.oldTeam,
        oldWorkshop: this.filterForm.oldWorkshop,
        pageNo: this.pageNo,
        pageSize: this.pageSize
      })
      if (res.code == 200) {
        this.isUpdate = true
        console.log('res', res)
        this.postchangeList = res.data.list
        this.total = res.data.total
      }
    },
    onSubmit() {
      console.log('submit!')
    }
  }
}
</script>

<style lang="scss" scoped>
.c-main-make {
  margin-left: 35px;
  height: 300px;
  background: #fff;
  margin-right: 35px;
  margin-bottom: 35px;
  position: relative;
  overflow: hidden;
  border: 1px #dcdee0 solid;
}
.c-main-show {
  min-height: 700px;
  background: #fff;
  margin-bottom: 35px;
  position: relative;
  overflow: hidden;
  border: 1px #dcdee0 solid;
}
.title-text {
  width: 100%;
  height: 50px;
  border-bottom: 1px #dcdee0 solid;
  font-family: '微软雅黑 Bold', '微软雅黑 Regular', 微软雅黑, sans-serif;
  font-weight: 700;
  color: #666666;
  line-height: 50px;
  padding-left: 25px;
}
.main-scroll {
  height: 1000px;
  // overflow-y: scroll;
}
.pagination {
  display: flex;
  justify-content: end;
}
.box-card {
  margin-bottom: 20px;
}
.el-input {
  width: 250px;
}
.el-select {
  width: 250px;
}
</style>
